package com.cnbot.toolkit.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.cnbot.toolkit.context.AppContextUtil;
import com.cnbot.toolkit.log.zlog.ZLog;

import java.io.IOException;

/**
 *  @项目名：  ToolKitModel
 *  @包名：    com.cnbot.toolkit.file
 *  @文件名:   NetUtil
 *  @创建者:   ww
 *  @创建时间:  2019/11/13 9:41
 *  @描述：    网络状态
 */
public final class NetUtil {

	private static final String TAG = "NetUtil";

	private NetUtil() {}

	public static boolean isNetworkConnected() {
		if (AppContextUtil.getApplicationContext() == null) {
			return false;
		}
		ConnectivityManager mConnectivityManager = (ConnectivityManager) AppContextUtil.getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		if (mNetworkInfo != null) {
			return mNetworkInfo.isAvailable();
		}
		return false;
	}

	public static boolean isWifiConnected() {
		if (AppContextUtil.getApplicationContext() == null) {
			return false;
		}
		ConnectivityManager mConnectivityManager = (ConnectivityManager) AppContextUtil.getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mWiFiNetworkInfo != null) {
			return mWiFiNetworkInfo.isAvailable();
		}
		return false;
	}

	public static boolean isMobileConnected() {
		if (AppContextUtil.getApplicationContext() == null) {
			return false;
		}
		ConnectivityManager mConnectivityManager = (ConnectivityManager) AppContextUtil.getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (mMobileNetworkInfo != null) {
			return mMobileNetworkInfo.isAvailable();
		}
		return false;
	}

	public static int getConnectedType() {
		if (AppContextUtil.getApplicationContext() == null) {
			return -1;
		}
		ConnectivityManager mConnectivityManager = (ConnectivityManager) AppContextUtil.getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
			return mNetworkInfo.getType();
		}
		return -1;
	}

	/**
	 * ping百度，防止网络已连接但是网络不可用的情况
	 * ping 2 次 ，超时5秒
	 * @return
	 */
	private static boolean isNetworkOnline() {
		Runtime runtime = Runtime.getRuntime();
		try {
			Process ipProcess = runtime.exec("ping -c 2 -w 5 www.baidu.com");
			int exitValue = ipProcess.waitFor();
			ZLog.wtf(TAG, "isNetworkOnline-" + exitValue);
			return (exitValue == 0);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 判断是否联网
	 * @param listener 网络监听接口
	 * @return
	 */
	public static Runnable isNetworkConnected(final INetCheckListener listener) {
		if (listener == null)
			return null;
		return new Runnable() {
			@Override
			public void run() {
				if (isNetworkConnected() && isNetworkOnline()) {
					listener.onNetAvailable();
				} else {
					listener.onNetDisable();
				}
			}
		};
	}

	/**
	 * @author ww
	 * 检测当前网络是否可用的接口回调
	 */
	public interface INetCheckListener {
		/**
		 * 网络可用
		 */
		void onNetAvailable();

		/**
		 * 网络不可用
		 */
		void onNetDisable();
	}
}
